#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved.
# description: the script that make install obs
# date: 2022-06-30
set -e

#----------------------- variables --------------------#
ARCH=`uname -m`
if [ ${ARCH} = "aarch64" ];then
        export BUILD_FOR_ARM=true
fi
G_CWD=`dirname $0`
pushd ${G_CWD} >/dev/null
G_CWD=`pwd`
popd >/dev/null

G_FILE_NAME=$0
G_BUILD_OPTION=release
G_BUILD_DIR=${G_CWD}
g_PATH=build




NGHTTP_DIR=${TRUNK_DIR}/dependency/nghttp2/install_comm
LIBXML2_DIR=${TRUNK_DIR}/dependency/libxml2/install_comm
CURL_DIR=${TRUNK_DIR}/output/kernel/dependency/libcurl/comm
OPENSSL_DIR=${TRUNK_DIR}/output/kernel/dependency/openssl/comm
PCRE_DIR=${TRUNK_DIR}/dependency/pcre/install_comm
LIBICONV_DIR=${TRUNK_DIR}/dependency/libiconv/install_comm
SECUREC_DIR=${TRUNK_DIR}/output/kernel/platform/Huawei_Secure_C/comm
#environment
export LD_LIBRARY_PATH=$OPENSSL_DIR/lib:$CURL_DIR/lib:$LIBICONV_DIR/lib:$PCRE_DIR/lib:$NGHTTP_DIR/lib:$LIBXML2_DIR/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$OPENSSL_DIR/lib:$CURL_DIR/lib:$LIBICONV_DIR/lib:$PCRE_DIR/lib:$NGHTTP_DIR/lib:$LIBXML2_DIR/lib:$LIBRARY_PATH
export C_INCLUDE_PATH=$OPENSSL_DIR/include:$CURL_DIR/include:$LIBICONV_DIR/include:$PCRE_DIR/include:$NGHTTP_DIR/include:$LIBXML2_DIR/include:$C_INCLUDE_PATH
export CXX_INCLUDE_PATH=$OPENSSL_DIR/include:$CURL_DIR/include:$LIBICONV_DIR/include:$PCRE_DIR/include:$NGHTTP_DIR/include:$LIBXML2_DIR/include:$CXX_INCLUDE_PATH

#----------------------- functions ---------------------#
L_PACKAGE_NAME=$1
L_PRODUCT_TYPE=`echo $2 | tr A-Z a-z`
L_PRODUCT=`echo $3 | tr A-Z a-z`

if [ "debug" == "$2" ];then
    G_BUILD_OPTION=debug
        export DEBUG=debug
fi

export openssl_version=openssl-1.1.1k
export curl_version=curl-7.78.0
export nghttp2_version=nghttp2-1.43.0
export NGHTTP2_LIBS="-L${NGHTTP_DIR}/lib -lnghttp2"
export NGHTTP2_CFLAGS=-I${NGHTTP_DIR}/include
export LIBXML2_LIBS="-L${LIBXML2_DIR}/lib -lxml2 -lm -ldl"
export LIBXML2_CFLAGS=-I${LIBXML2_DIR}/include/libxml2
export LIBOPENSSL_LIBS="-L${OPENSSL_DIR}/lib -lssl -lcrypto"
export LIBOPENSSL_CFLAGS=-I${OPENSSL_DIR}/include
export LIBPCRE_LIBS="-L${PCRE_DIR}/lib -lpcre"
export LIBPCRE_CFLAGS=-I${PCRE_DIR}/include
export LIBSECUREC_LIBS="-L${SECUREC_DIR}/lib -lsecurec"
export LIBSECUREC_CFLAGS=-I${SECUREC_DIR}/include
export LIBICONV_CFLAGS=-I${LIBICONV_DIR}/include
export LIBICONV_LIBS="-L${LIBICONV_DIR}/lib  -liconv"

function build_obs()
{
	make clean
	make

	if [ -d demo ];then
		rm -rf demo
	fi
	if [ -d include ];then
		rm -rf include
	fi
	if [ -d lib ];then
		rm -rf lib
	fi
	mkdir demo
	mkdir include
	mkdir lib

	if [ "$G_BUILD_OPTION" == "debug" ];then
	g_PATH=build-debug
	fi

	rm -rf {demo,include,lib,"Log Collection Statement.txt"}
	make -j16
}


